<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>距离元旦还有<span id="time"></span></h1>
    <script>


        // 1、 封装函数   格式化日期(formatDate)

        function formatDate() {
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var week = date.getDay();
            var hours = date.getHours();
            var mins = date.getMinutes();
            var secs = date.getSeconds();
            var arr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            return year + "-" + month + "-" + day + " " + arr[week] + " " + hours + ":" + mins + ":" + secs + "<br/>";
        }

        document.write(formatDate());

        // 2、 封装函数  求上个月的天数

        function getmonthday(year, month) {
            // var date = new Date();
            // var year = date.getFullYear();
            // var month = date.getMonth() + 1;

            // 此时month=12;但是天数为0是会使月份以12-1=11，即计算当月天数
            var d = new Date(year, month, 0);
            return year + "年" + month + "月" + "有" + d.getDate() + "天" + "<br/>";
        }
        // console.log(getmonthday(2021, 11));
        document.write(getmonthday(2021, 11));

        // 3、 封装函数  求本月的天数

        function getmonthday(year, month) {
            var d = new Date(year, month, 0);
            return year + "年" + month + "月" + "有" + d.getDate() + "天" + "<br/>";
        }
        // console.log(getmonthday(2021, 12));
        document.write(getmonthday(2021, 12))


        // 4、 封装函数 距离元旦还有多少天，小时，分钟，秒  --- 倒计时

        var time = document.querySelector("span");
        var tt = new Date('2022-1-1 0:0:0')
        showDate(tt)

        function showDate(t) {
            var future = new Date(t);
            var now = new Date();
            // 时间是可以相减的  --- 毫秒数
            var gap = future - now;
            // console.log(gap);
            var allSeconds = parseInt(gap / 1000);
            // console.log(allSeconds);

            if (allSeconds <= 0) {
                time.innerHTML = '时间已过期了'
                return
            }

            var day = parseInt(allSeconds / 3600 / 24)
            var h = parseInt(allSeconds / 3600 - day * 24);
            var m = parseInt(allSeconds % 3600 / 60);
            var s = allSeconds % 60;


            // console.log(day + "天" + h + "时" + m + "分" + s + "秒");
            time.innerHTML = day + "天" + h + "时" + m + "分" + s + "秒";
        }

        var t = setInterval(function () {
            showDate(tt);
            console.log(allSeconds);
            if (allSeconds <= 0) {
                // console.log(666);
                // 清除倒计时
                clearInterval(t)
            }
            // 隔多少毫秒执行函数
        }, 1000)



    </script>
</body>

</html>